package com.trimps.steps.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hotfire.template.common.Paginator;
import com.trimps.steps.exception.SqlOperationsException;
import com.trimps.steps.pojo.Walker;
import com.trimps.steps.pojo.WalkerGroup;
import com.trimps.steps.service.IWalkerGroupService;
import com.trimps.steps.service.IWalkerService;

@Controller
@RequestMapping(value = "walker/")
public class WalkerController extends BaseController {
    private static final Logger logger = Logger.getLogger(WalkerController.class);

    @Autowired
    private IWalkerService walkerService;
    @Autowired
    private IWalkerGroupService walkerGroupService;

    /**
     * 
     * @param page
     * @param map   必须这样写：@RequestParam HashMap<String, String> map,否则无法映射值, jsp页面可以通过 ${map.myname }获取
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "list")
    public String list(Paginator page, 
                    @RequestParam(required=false) String realyName,
                    @RequestParam(required=false) String groupId,
                    ModelMap modelMap) {
        
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("realyName", realyName);
        map.put("groupId", groupId);
        
        if(page == null || page.getTotalItemCount() == -1){
            page = new Paginator();
            page.setShowItemCount(8);
            int userCount = walkerService.getWalkerCount(map);
            page.setTotalItemCount(userCount);
        }
        
        List<Walker> walkers = walkerService.getWalker(page, map);
        List<WalkerGroup> groups = walkerGroupService.getAllWalkerGroups();
        modelMap.put("walkers", walkers);
        modelMap.put("groups", groups);
        modelMap.put("page", page);
        modelMap.put("map", map);
        
        return "walker/walkerlist";
    }

    @RequestMapping(value = "doSave")
    @ResponseBody
    public Map<String, Object> doSave(Walker walker,
                    ModelMap modelMap) {
        try {
            int id = walkerService.save(walker);
            if(id > 0){
                return super.buildSuccessMsg("保存成功！");
            } 
        } catch (SqlOperationsException e) {
            return super.buildFailureMsg(e.getMessage());
        }
        
        return super.buildFailureMsg("保存失败！");
    }
    
    @RequestMapping(value = "doDel")
    public String del(HttpServletRequest request, 
                    @RequestParam(required=true) int id,
                    ModelMap modelMap) {
        try {
            int ret = walkerService.delById(id);
            if(ret <= 0){
                return super.sendErrorPage(request, "删除成员失败！");
            }
        } catch (SqlOperationsException e) {
            return super.sendErrorPage(request, "删除成员失败："+e.getMessage());
        }
        
        return "redirect:list";
    }
    
    @RequestMapping(value = "getOwnGroup")
    @ResponseBody
    public List<WalkerGroup> getOwnGroup() {
        if(getUserId() != null){
            return walkerGroupService.getWalkerGroupByUserId(getUserId());
        }
        return null;
    }
    
    @RequestMapping(value = "getWalkersByGroupId")
    @ResponseBody
    public List<Walker> getWalkersByGroupId(@RequestParam(required=true) int groupId) {
        return walkerService.getWalkersByGroupId(groupId);
    }
}
